package com.scau.os.fileSystem.FolderTree;

import java.util.List;

import com.scau.os.extension.EventSystem;
import com.scau.os.fileSystem.directory.DirectoryEntry;
import com.scau.os.fileSystem.other.MyFileSystemException;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Callback;

public class FolderPane extends BorderPane {
	private static FolderPane instance = new FolderPane();

	private FolderPane() {
		init();
	}

	public static FolderPane getInstance() {
		return instance;
	}

	private HBox titleBox; // 标题栏
	private Label titleLabel; // 标题
	private TreeView<DirectoryEntry> folderTree; // 目录树
	private boolean isFound;

	public void init() {
		titleLabel = new Label("我的电脑"); // 创建标题
		titleLabel.setFont(new Font("Microsoft YaHei", 15.0)); // 设置字体
		titleLabel.setTextFill(Color.BLACK); // 设置颜色

		titleBox = new HBox(); // 创建标题栏
		titleBox.setPadding(new Insets(7));
		titleBox.getChildren().add(titleLabel); // 将标题添加到标题栏中

		FolderTreeItem root = new FolderTreeItem(DirectoryEntry.ROOT_DIRECTORY); // 创建根节点
		try {
			root.preLoad();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		folderTree = new TreeView<>(root); // 创建目录树
		folderTree.setShowRoot(false); // 不显示根节点
		folderTree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); // 设置选择模式为单选

		folderTree.setCellFactory(new Callback<TreeView<DirectoryEntry>, TreeCell<DirectoryEntry>>() {
			@Override
			public TreeCell<DirectoryEntry> call(TreeView<DirectoryEntry> treeView) {
				return new TreeCell<DirectoryEntry>() {

					@Override
					protected void updateItem(DirectoryEntry item, boolean empty) {
						super.updateItem(item, empty);

						if (empty || item == null) { // 如果为空或者item不存在
							setText(null);
							setGraphic(null);
						} else {
							String text = item.getProperty().getName();
							if (item.isFile()) {
								text += "." + item.getProperty().getType();
							} // 获取文件系统的显示名称

							setText(text); // 设置显示名称
						}

					}

				};
			}
		});

		// 获取选中的当前项目
		folderTree.getSelectionModel().selectedItemProperty()
				.addListener(new ChangeListener<TreeItem<DirectoryEntry>>() {
					@Override
					public void changed(ObservableValue<? extends TreeItem<DirectoryEntry>> observableValue,
							TreeItem<DirectoryEntry> oldItem, TreeItem<DirectoryEntry> newItem) {
						if (oldItem != null) {
							System.out.println("oldItem is " + oldItem.getValue().getPath().getAbsolutePath());
						} else {
							System.out.println("oldItem is null");
						}

						if (newItem != null) {
							System.out.println("newItem is " + newItem.getValue().getPath().getAbsolutePath());
						} else {
							System.out.println("newItem is null");
							return;
						}
						EventSystem.instance.SendEvent("getSelectedTreeItem", newItem.getValue());
					}
				});

		this.setTop(titleBox); // 将标题栏添加到顶部
		this.setCenter(folderTree); // 将目录树添加到中心

		folderTree.getSelectionModel().clearSelection();
		folderTree.getSelectionModel().select(root); // 选中根节点
	}

	private void searchDirectory(TreeItem<DirectoryEntry> root, List<String> directoryName, int index)
			throws Exception {

		if (root.getValue().isDirectory() == false) {
			return;
		}

		if (root.getValue().getProperty().getName().trim().equals(directoryName.get(index).trim())) {
			if (index == directoryName.size() - 1) {
				isFound = true;
				folderTree.getSelectionModel().select(root);
				return;
			}
			for (TreeItem<DirectoryEntry> child : root.getChildren()) {
				searchDirectory(child, directoryName, index + 1);
			}
		}

	}

	public void refresh(DirectoryEntry directory) throws Exception {
		isFound = false;
		if (directory.equals(DirectoryEntry.ROOT_DIRECTORY)) {
			folderTree.getSelectionModel().select(folderTree.getRoot());
			isFound = true;
		} else {
			for (TreeItem<DirectoryEntry> child : folderTree.getRoot().getChildren()) {
				searchDirectory(child, directory.getPath().getPathNames(), 0);
			}
		}

		if (isFound == false) {
			throw new MyFileSystemException("目录不存在");
		}

		FolderTreeItem currentDirectory = (FolderTreeItem) folderTree.getSelectionModel().getSelectedItem();
		currentDirectory.setLoaded(false);
		currentDirectory.getChildren().clear();
		currentDirectory.preLoad();

		folderTree.getSelectionModel().clearSelection();
		folderTree.getSelectionModel().select(currentDirectory);
	}

	public TreeView<DirectoryEntry> getFolderTree() {
		return folderTree;
	}
}